# coding=utf-8

"""
基于JSON文件，使用pyecharts生成系统关系图

@author:李志强 JOHNNY LI
@2021.8
@shanghai

ref：使用 pyecharts 1.1.0
参考地址: https://echarts.apache.org/examples/editor.html?c=graph-npm
"""

import pyecharts.options as opts
from pyecharts.charts import Graph
import json

json_file = "all_node_data.json"

with open(json_file, 'r') as f:
    data_local = f.read()
    data = json.loads(data_local)

nodes = [
    {
        "x": node["x"],
        "y": node["y"],
        "id": node["id"],
        "name": node["label"],
        "symbolSize": node["size"],
        "itemStyle": {"normal": {"color": node["color"]}},
    }
    for node in data["nodes"]
]

nodes2 = [
    {
        # "x": node["x"],
        # "y": node["y"],
        "id": node["id"],
        "name": node["label"],
        "symbolSize": node["size"],
        "symbol": node["symbol"],
        # "itemStyle": {"normal": {"color": node["color"]}},
        "category": node["category"]
    }
    for node in data["nodes"]
]

edges = [
    {"source": edge["sourceID"], "target": edge["targetID"], "value": edge["value"],
     "linestyle_opts": opts.LineStyleOpts(type_=edge["type_"]),
     } for
    edge in data["edges"]
]

categories = data["categories"]

(
    Graph(init_opts=opts.InitOpts(width="1600px", height="1200px"))
        .add(
        series_name="",
        nodes=nodes2,
        links=edges,
        categories=categories,
        layout="circular",  # circular , force, none
        is_roam=True,
        is_focusnode=True,
        repulsion=100,
        is_rotate_label=True,
        label_opts=opts.LabelOpts(is_show=True, position="right"),
        linestyle_opts=opts.LineStyleOpts(width=1, curve=0.3, opacity=1.0),  # type_="solid"
        edge_symbol=["", "arrow"],  # 单向箭头
        edge_label=opts.LabelOpts(
            is_show=False, position="middle", formatter="{b}: {c} "  # 设置关系说明
        )
    )
        .set_global_opts(title_opts=opts.TitleOpts(title="系统数据关系图"),
                         legend_opts=opts.LegendOpts(orient="vertical", pos_left="2%", pos_top="20%"), )
        .render("relations-systems.html")
)
